package com.standardoperationapp.activity;

import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.rfid.trans.ReaderParameter;
import com.standardoperationapp.app.BaseApplication;
import com.standardoperationapp.bluetooth.BluetoothManger;
import com.standardoperationapp.global.GlobalVaries;
import com.standardoperationapp.R;
import com.standardoperationapp.global.SharedPreferencesUtils;
import com.standardoperationapp.materialinventory.activity.CommnonActivity;
import com.standardoperationapp.materialinventory.activity.ListItemActivity;
import com.standardoperationapp.operationplan.MyBroadcastReceiver;
import com.standardoperationapp.operationplan.OperationPlanActivity;
import com.standardoperationapp.utils.OtgUtils;
import com.standardoperationapp.utils.Reader;
import com.standardoperationapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button xiaohaoBtn, lingyongBtn, diaoboBtn, kucunbuchongBtn,
            pandianBtn, xianchangwuziBtn, zuoyejihuaBtn, tongdaomenBtn;


    // 是否使用rfid
    boolean userRfid = false;
    private MyBroadcastReceiver liaisonManBroadcastReceiver;
    //蓝牙变量


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //syj 改变位置 只初始化一次

        GlobalVaries.initVaries();
        initView();

        //syj blue test


        //    }

        liaisonManBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.yl.dmr.ptt.keydown");
        //注冊showPowerReceiver
        registerReceiver(liaisonManBroadcastReceiver, filter);

    }

    public void initView() {
        userRfid = SharedPreferencesUtils.getBooleanSharedPreferencesData(BaseApplication.app, "userRfid", false);


        xiaohaoBtn = (Button) findViewById(R.id.xiaohaoBtn);
        xiaohaoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                turn2Common();
            }
        });
        lingyongBtn = (Button) findViewById(R.id.lingyongBtn);
        lingyongBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.LINGYONG;
                turn2Common();
            }
        });
        diaoboBtn = (Button) findViewById(R.id.diaoboBtn);
        diaoboBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.DIAOBO;
                turn2Common();
            }
        });
        kucunbuchongBtn = (Button) findViewById(R.id.kucunbuchongBtn);
        kucunbuchongBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.KUCUNBUCHONG;
                turn2Common();
            }
        });
        pandianBtn = (Button) findViewById(R.id.pandianBtn);
        pandianBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.PANDIAN;
                turn2Common();
            }
        });
        xianchangwuziBtn = (Button) findViewById(R.id.xianchangwuziBtn);
        xianchangwuziBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.XIANCHANGWUZI;
                turn2Common();
            }
        });
        findViewById(R.id.matBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turn2Common();
            }
        });
        zuoyejihuaBtn = (Button) findViewById(R.id.zuoyejihuaBtn);
        zuoyejihuaBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, OperationPlanActivity.class);
                startActivity(intent);
            }
        });
        tongdaomenBtn = (Button) findViewById(R.id.tongdaomenBtn);
        tongdaomenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.uiType = GlobalVaries.DOOR_MANAGE;
                Intent intent = new Intent(MainActivity.this, BluetoothSetActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.rfidBtn).setOnClickListener(view -> {
            if (!userRfid) {
                ToastUtil.showToast("请在登录页面启用RFID功能");
                return;
            }
            try {
                if (Reader.rrlib.isConnect()) {
                    startActivity(new Intent(this, RFIDWriteActivity.class));
                } else {
                    int result = Reader.rrlib.Connect(devport, 57600, 1);
                    if (result == 0) {
                        ReaderParameter param = Reader.rrlib.GetInventoryPatameter();
                        param.Session = 255;
                        startActivity(new Intent(this, RFIDWriteActivity.class));
                    } else {
                        result = Reader.rrlib.Connect(devport, 115200, 1);
                        if (result == 0) {
                            ReaderParameter param = Reader.rrlib.GetInventoryPatameter();
                            param.Session = 255;
                            startActivity(new Intent(this, RFIDWriteActivity.class));
                        } else {
                            ToastUtil.showToast("打开串口失败");
                        }
                    }
                }
            } catch (Exception e) {
                ToastUtil.showToast("连接串口报错了：" + e.toString());
            }

        });
    }

    public void turn2Common() {
        Intent intent = new Intent(MainActivity.this, CommnonActivity.class);
        startActivity(intent);

    }

    private static String devport = "/dev/ttyHSL0";//RR
    @Override
    protected void onResume() {
        if (userRfid) {
            mVirtualKeyListenerBroadcastReceiver = new VirtualKeyListenerBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            this.registerReceiver(mVirtualKeyListenerBroadcastReceiver, intentFilter);
            OtgUtils.set53GPIOEnabled(true);
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (userRfid) {
            OtgUtils.set53GPIOEnabled(false);
            this.unregisterReceiver(mVirtualKeyListenerBroadcastReceiver);
            Reader.rrlib.DisConnect();
        }

        if (liaisonManBroadcastReceiver != null) {
            unregisterReceiver(liaisonManBroadcastReceiver);
        }
        super.onDestroy();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    private VirtualKeyListenerBroadcastReceiver mVirtualKeyListenerBroadcastReceiver;
    public  static boolean mSwitchFlag = false;
    private class VirtualKeyListenerBroadcastReceiver extends BroadcastReceiver {
        private final String SYSTEM_REASON = "reason";
        private final String SYSTEM_HOME_KEY = "homekey";
        private final String SYSTEM_RECENT_APPS = "recentapps";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String systemReason = intent.getStringExtra(SYSTEM_REASON);
                if (systemReason != null) {
                    mSwitchFlag = true;
                    if (systemReason.equals(SYSTEM_HOME_KEY)) {
                        System.out.println("Press HOME key");
                        OtgUtils.set53GPIOEnabled(false);
                    } else if (systemReason.equals(SYSTEM_RECENT_APPS)) {
                        System.out.println("Press RECENT_APPS key");
                        OtgUtils.set53GPIOEnabled(true);
                        //Reader.rrlib.PowerControll(null,false);
                        //mSwitchFlag = true;
                    }

                }
            }
        }
    }
}